package com.xia.manager.controller;

import com.cy.entity.Product;
import com.xia.manager.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName ProductController
 * @Deserintion 产品controller
 * @Author ciyuan
 * @Date 2018/11/25/025 1:23
 **/
@Api(value = "产品controller",tags = {"产品操作接口"})
@RestController
@RequestMapping("/products")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService service;

    /**
     * 添加产品
     * @param product
     * @return
     */
    @ApiOperation(value = "创建产品",notes = "根据对应业务规则添加相应产品")
    @PostMapping("")
    public Product addProduct(@RequestBody Product product){
        log.info("创建产品，参数【{}】",product);

        Product result = service.addProduct(product);

        log.info("创建产品，结果【{}】",result);
        return result;
    }

    /**
     * 查询单个产品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product findOne(@PathVariable String id){
        log.info("查询单个产品，id=【{}】",id);
        Product result = service.findOne(id);
        log.info("查询单个产品，结果=【{}】", result);
        return result;
    }

    @GetMapping("")
    public Page<Product> query(String ids, BigDecimal minRewardRate, BigDecimal maxRewardRate, String status,
                               @RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        log.info("查询产品，ids=【{}】，minRewardRate=【{}】，maxRewardRate=【{}】，status=【{}】，pageNum=【{}】，pageSize=【{}】",ids,minRewardRate,maxRewardRate,status,pageNum,pageSize);
        List<String> idList = null,statusList = null;
        if (!StringUtils.isEmpty(ids)) {
            idList = Arrays.asList(ids.split(","));
        }
        if (!StringUtils.isEmpty(status)){
            statusList = Arrays.asList(status.split(","));
        }
        Pageable pageable = new PageRequest(pageNum,pageSize);
        Page<Product> page = service.query(idList, minRewardRate, maxRewardRate, statusList, pageable);
        log.info("查询产品，结果=【{}】",page);
        return page;
    }
}
